package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.SysUserDeviceBind;
import com.ruoyi.system.domain.vo.IndexVo;
import com.ruoyi.system.mapper.SysUserDeviceBindMapper;
import com.ruoyi.system.service.ISysUserDeviceBindService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 用户设备绑定记录 服务层实现
 *
 * @author 10100
 */
@Service
public class SysUserDeviceBindServiceImpl implements ISysUserDeviceBindService {
    private final SysUserDeviceBindMapper sysUserDeviceBindMapper;

    public SysUserDeviceBindServiceImpl(SysUserDeviceBindMapper sysUserDeviceBindMapper) {
        this.sysUserDeviceBindMapper = sysUserDeviceBindMapper;
    }

    @Override
    public SysUserDeviceBind selectSysUserDeviceBindById(Long id) {
        return sysUserDeviceBindMapper.selectSysUserDeviceBindById(id);
    }

    @Override
    public List<SysUserDeviceBind> selectSysUserDeviceBindList(SysUserDeviceBind sysUserDeviceBind) {
        return sysUserDeviceBindMapper.selectSysUserDeviceBindList(sysUserDeviceBind);
    }

    @Override
    public int insertSysUserDeviceBind(SysUserDeviceBind sysUserDeviceBind) {
        return sysUserDeviceBindMapper.insertSysUserDeviceBind(sysUserDeviceBind);
    }

    @Override
    public int updateSysUserDeviceBind(SysUserDeviceBind sysUserDeviceBind) {
        return sysUserDeviceBindMapper.updateSysUserDeviceBind(sysUserDeviceBind);
    }

    @Override
    public int deleteSysUserDeviceBindById(Long id) {
        return sysUserDeviceBindMapper.deleteSysUserDeviceBindById(id);
    }

    @Override
    public int deleteSysUserDeviceBindByIds(Long[] ids) {
        return sysUserDeviceBindMapper.deleteSysUserDeviceBindByIds(ids);
    }

    @Override
    public void recordUserDeviceBind(Long userId, Long deviceId) {
        // 先查询是否存在未解绑的记录
        SysUserDeviceBind query = new SysUserDeviceBind();
        query.setUserId(userId);
        query.setDeviceId(deviceId);
        query.setStatus("0");
        List<SysUserDeviceBind> existingBinds = sysUserDeviceBindMapper.selectSysUserDeviceBindList(query);

        // 如果存在未解绑的记录，先解绑
        if (!existingBinds.isEmpty()) {
            for (SysUserDeviceBind bind : existingBinds) {
                bind.setStatus("1");
                bind.setUnbindTime(new Date());
                sysUserDeviceBindMapper.updateSysUserDeviceBind(bind);
            }
        }

        // 创建新的绑定记录
        SysUserDeviceBind newBind = new SysUserDeviceBind();
        newBind.setUserId(userId);
        newBind.setDeviceId(deviceId);
        newBind.setBindTime(new Date());
        newBind.setStatus("0");
        newBind.setCreateTime(new Date());
        newBind.setUpdateTime(new Date());

        sysUserDeviceBindMapper.insertSysUserDeviceBind(newBind);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void recordUserDeviceUnbind(Long userId, Long deviceId) {
        // 查询未解绑的记录
        SysUserDeviceBind query = new SysUserDeviceBind();
        query.setUserId(userId);
        query.setDeviceId(deviceId);
        query.setStatus("0");
        List<SysUserDeviceBind> existingBinds = sysUserDeviceBindMapper.selectSysUserDeviceBindList(query);

        if (existingBinds.isEmpty()) {
            return;
        }

        // 更新所有未解绑的记录
        for (SysUserDeviceBind bind : existingBinds) {
            bind.setStatus("1");
            bind.setUnbindTime(new Date());
            bind.setUpdateTime(new Date());
            sysUserDeviceBindMapper.updateSysUserDeviceBind(bind);
        }
    }

    @Override
    public List<IndexVo> selectUserBindHourlyStats() {
        return sysUserDeviceBindMapper.selectUserBindHourlyStats();
    }

    @Override
    public List<IndexVo> selectUserBindDailyStats() {
        return sysUserDeviceBindMapper.selectUserBindDailyStats();
    }
} 